<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>菜单管理</title>
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css" media="all">
<link rel="stylesheet" href="__PUBLIC__/admin/css/admin.css">
<link rel="stylesheet" type="text/css"
	href="__PUBLIC__/admin/css/font-awesome.min.css">
</head>
<body class="layui-bg-gray">
	<div class="layui-fluid">
		<div class="layui-row layui-col-space10">
			<div class="layui-col-md12 layui-col-xs12">
				<div class="layui-card">
					<div class="layui-card-header">城市列表</div>
					<div class="layui-card-body">
						<button id="addBtn"
							class="layui-btn layui-btn-primary layui-btn-sm">
							<i class="fa fa-plus-circle"></i> 添加城市
						</button>
						<div id="regions"></div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script src="__PUBLIC__/layui/layui.js"></script>
	<script type="text/javascript"
		src="__PUBLIC__/common/js/jquery-2.0.3.min.js"></script>
	<script type="text/javascript">
		$(function() {
			$("#addBtn").click(function() {
				parent.layer.open({
					type : 2,
					title : "添加菜单",
					area : [ '500px', '80%' ],
					content : "{:U('Settings/addRegion')}",
					cancel : function(index, layero) {
						var cache = layui.data('cache');
						var status = cache.status;
						layui.data('cache', {
							key : 'status',
							value : null
						});
						if (status) {
							location.reload();
						}
					}
				});
			});
		});
	</script>
	<script type="text/javascript">
		function addSubCity(id, name) {
			parent.layer.open({
				type : 2,
				title : '添加城市数据',
				area : [ '500px', '80%' ],
				content : "{:U('Settings/addRegion')}?pid=" + id,
				cancel : function(index, layero) {
					var cache = layui.data('cache');
					var status = cache.status;
					layui.data('cache', {
						key : 'status',
						value : null
					});
					if (status) {
						location.reload();
					}
				}
			});
		}
		function editCity(id) {
			parent.layer.open({
				type : 2,
				title : '修改城市数据',
				area : [ '500px', '80%' ],
				content : "{:U('Settings/editRegion')}?nid=" + id,
				cancel : function(index, layero) {
					var cache = layui.data('cache');
					var status = cache.status;
					layui.data('cache', {
						key : 'status',
						value : null
					});
					if (status) {
						location.reload();
					}
				}
			});
		}
		var layout = [
				{
					name : '名称',
					treeNodes : true,
					headerClass : 'value_col',
					colClass : 'value_col',
					style : ''
				},
				{
					name : '操作',
					headerClass : 'value_col',
					colClass : 'value_col',
					style : '',
					render : function(row) {
						var desc = '';
						if (row.level == 1) {
							desc = '添加市县';
						} else if (row.level == 2) {
							desc = '添加城区';
						}
						var add = '<a class="layui-btn layui-btn layui-btn-xs" onclick="addSubCity('
								+ row.id
								+ ',\''
								+ row.name
								+ '\')"><i class="layui-icon">&#xe654;</i>'
								+ desc + '</a>';
						var edit = '<a class="layui-btn layui-btn-info layui-btn-xs" onclick="editCity('
								+ row.id
								+ ')"><i class="layui-icon">&#xe642;</i>编辑</a>';
						return add + edit;
					}
				} ];

		layui.use([ 'form', 'tree', 'layer' ], function() {
			var layer = layui.layer, form = layui.form, $ = layui.jquery;
			$.ajax({
				type : 'POST',
				url : "{:U('Settings/regions')}",
				data : {
					method : 'list'
				},
				success : function(res) {
					layui.treeGird({
						elem : "#regions",
						spreadable : false,
						nodes : res,
						layout : layout
					});
				}
			})
		});
	</script>
</body>
</html>